{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "improving_neural_net_performance.ipynb",
      "version": "0.3.2",
      "views": {},
      "default_view": {},
      "provenance": [],
      "collapsed_sections": [
        "jFfc3saSxg6t",
        "FSPZIiYgyh93",
        "GhFtWjQRzD2l",
        "P8BLQ7T71JWd",
        "copyright-notice"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "source": [
        "#### Copyright 2017 Google LLC."
      ],
      "metadata": {
        "id": "copyright-notice",
        "colab_type": "text"
      },
      "cell_type": "markdown"
    },
    {
      "execution_count": 0,
      "source": [
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ],
      "metadata": {
        "cellView": "both",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "id": "copyright-notice2",
        "colab_type": "code"
      },
      "cell_type": "code",
      "outputs": []
    },
    {
      "metadata": {
        "id": "eV16J6oUY-HN",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " # \u63d0\u9ad8\u795e\u7ecf\u7f51\u7edc\u6027\u80fd"
      ]
    },
    {
      "metadata": {
        "id": "0Rwl1iXIKxkm",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " **\u5b66\u4e60\u76ee\u6807**\uff1a\u901a\u8fc7\u5c06\u7279\u5f81\u6807\u51c6\u5316\u5e76\u5e94\u7528\u5404\u79cd\u4f18\u5316\u7b97\u6cd5\u6765\u63d0\u9ad8\u795e\u7ecf\u7f51\u7edc\u7684\u6027\u80fd\n",
        "\n",
        "**\u6ce8\u610f**\uff1a\u672c\u7ec3\u4e60\u4e2d\u4ecb\u7ecd\u7684\u4f18\u5316\u65b9\u6cd5\u5e76\u975e\u4e13\u95e8\u9488\u5bf9\u795e\u7ecf\u7f51\u7edc\uff1b\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u6709\u6548\u6539\u8fdb\u5927\u591a\u6570\u7c7b\u578b\u7684\u6a21\u578b\u3002"
      ]
    },
    {
      "metadata": {
        "id": "lBPTONWzKxkn",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ## \u8bbe\u7f6e\n",
        "\n",
        "\u9996\u5148\uff0c\u6211\u4eec\u5c06\u52a0\u8f7d\u6570\u636e\u3002"
      ]
    },
    {
      "metadata": {
        "id": "VtYVuONUKxko",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "from __future__ import print_function\n",
        "\n",
        "import math\n",
        "\n",
        "from IPython import display\n",
        "from matplotlib import cm\n",
        "from matplotlib import gridspec\n",
        "from matplotlib import pyplot as plt\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn import metrics\n",
        "import tensorflow as tf\n",
        "from tensorflow.python.data import Dataset\n",
        "\n",
        "tf.logging.set_verbosity(tf.logging.ERROR)\n",
        "pd.options.display.max_rows = 10\n",
        "pd.options.display.float_format = '{:.1f}'.format\n",
        "\n",
        "california_housing_dataframe = pd.read_csv(\"https://download.mlcc.google.cn/mledu-datasets/california_housing_train.csv\", sep=\",\")\n",
        "\n",
        "california_housing_dataframe = california_housing_dataframe.reindex(\n",
        "    np.random.permutation(california_housing_dataframe.index))"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "B8qC-jTIKxkr",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def preprocess_features(california_housing_dataframe):\n",
        "  \"\"\"Prepares input features from California housing data set.\n",
        "\n",
        "  Args:\n",
        "    california_housing_dataframe: A Pandas DataFrame expected to contain data\n",
        "      from the California housing data set.\n",
        "  Returns:\n",
        "    A DataFrame that contains the features to be used for the model, including\n",
        "    synthetic features.\n",
        "  \"\"\"\n",
        "  selected_features = california_housing_dataframe[\n",
        "    [\"latitude\",\n",
        "     \"longitude\",\n",
        "     \"housing_median_age\",\n",
        "     \"total_rooms\",\n",
        "     \"total_bedrooms\",\n",
        "     \"population\",\n",
        "     \"households\",\n",
        "     \"median_income\"]]\n",
        "  processed_features = selected_features.copy()\n",
        "  # Create a synthetic feature.\n",
        "  processed_features[\"rooms_per_person\"] = (\n",
        "    california_housing_dataframe[\"total_rooms\"] /\n",
        "    california_housing_dataframe[\"population\"])\n",
        "  return processed_features\n",
        "\n",
        "def preprocess_targets(california_housing_dataframe):\n",
        "  \"\"\"Prepares target features (i.e., labels) from California housing data set.\n",
        "\n",
        "  Args:\n",
        "    california_housing_dataframe: A Pandas DataFrame expected to contain data\n",
        "      from the California housing data set.\n",
        "  Returns:\n",
        "    A DataFrame that contains the target feature.\n",
        "  \"\"\"\n",
        "  output_targets = pd.DataFrame()\n",
        "  # Scale the target to be in units of thousands of dollars.\n",
        "  output_targets[\"median_house_value\"] = (\n",
        "    california_housing_dataframe[\"median_house_value\"] / 1000.0)\n",
        "  return output_targets"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Ah6LjMIJ2spZ",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "# Choose the first 12000 (out of 17000) examples for training.\n",
        "training_examples = preprocess_features(california_housing_dataframe.head(12000))\n",
        "training_targets = preprocess_targets(california_housing_dataframe.head(12000))\n",
        "\n",
        "# Choose the last 5000 (out of 17000) examples for validation.\n",
        "validation_examples = preprocess_features(california_housing_dataframe.tail(5000))\n",
        "validation_targets = preprocess_targets(california_housing_dataframe.tail(5000))\n",
        "\n",
        "# Double-check that we've done the right thing.\n",
        "print(\"Training examples summary:\")\n",
        "display.display(training_examples.describe())\n",
        "print(\"Validation examples summary:\")\n",
        "display.display(validation_examples.describe())\n",
        "\n",
        "print(\"Training targets summary:\")\n",
        "display.display(training_targets.describe())\n",
        "print(\"Validation targets summary:\")\n",
        "display.display(validation_targets.describe())"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "NqIbXxx222ea",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ## \u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\n",
        "\n",
        "\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u3002"
      ]
    },
    {
      "metadata": {
        "id": "6k3xYlSg27VB",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def construct_feature_columns(input_features):\n",
        "  \"\"\"Construct the TensorFlow Feature Columns.\n",
        "\n",
        "  Args:\n",
        "    input_features: The names of the numerical input features to use.\n",
        "  Returns:\n",
        "    A set of feature columns\n",
        "  \"\"\" \n",
        "  return set([tf.feature_column.numeric_column(my_feature)\n",
        "              for my_feature in input_features])"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "De9jwyy4wTUT",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):\n",
        "    \"\"\"Trains a neural network model.\n",
        "  \n",
        "    Args:\n",
        "      features: pandas DataFrame of features\n",
        "      targets: pandas DataFrame of targets\n",
        "      batch_size: Size of batches to be passed to the model\n",
        "      shuffle: True or False. Whether to shuffle the data.\n",
        "      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely\n",
        "    Returns:\n",
        "      Tuple of (features, labels) for next data batch\n",
        "    \"\"\"\n",
        "    \n",
        "    # Convert pandas data into a dict of np arrays.\n",
        "    features = {key:np.array(value) for key,value in dict(features).items()}                                           \n",
        " \n",
        "    # Construct a dataset, and configure batching/repeating.\n",
        "    ds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limit\n",
        "    ds = ds.batch(batch_size).repeat(num_epochs)\n",
        "    \n",
        "    # Shuffle the data, if specified.\n",
        "    if shuffle:\n",
        "      ds = ds.shuffle(10000)\n",
        "    \n",
        "    # Return the next batch of data.\n",
        "    features, labels = ds.make_one_shot_iterator().get_next()\n",
        "    return features, labels"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "W-51R3yIKxk4",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def train_nn_regression_model(\n",
        "    my_optimizer,\n",
        "    steps,\n",
        "    batch_size,\n",
        "    hidden_units,\n",
        "    training_examples,\n",
        "    training_targets,\n",
        "    validation_examples,\n",
        "    validation_targets):\n",
        "  \"\"\"Trains a neural network regression model.\n",
        "  \n",
        "  In addition to training, this function also prints training progress information,\n",
        "  as well as a plot of the training and validation loss over time.\n",
        "  \n",
        "  Args:\n",
        "    my_optimizer: An instance of `tf.train.Optimizer`, the optimizer to use.\n",
        "    steps: A non-zero `int`, the total number of training steps. A training step\n",
        "      consists of a forward and backward pass using a single batch.\n",
        "    batch_size: A non-zero `int`, the batch size.\n",
        "    hidden_units: A `list` of int values, specifying the number of neurons in each layer.\n",
        "    training_examples: A `DataFrame` containing one or more columns from\n",
        "      `california_housing_dataframe` to use as input features for training.\n",
        "    training_targets: A `DataFrame` containing exactly one column from\n",
        "      `california_housing_dataframe` to use as target for training.\n",
        "    validation_examples: A `DataFrame` containing one or more columns from\n",
        "      `california_housing_dataframe` to use as input features for validation.\n",
        "    validation_targets: A `DataFrame` containing exactly one column from\n",
        "      `california_housing_dataframe` to use as target for validation.\n",
        "      \n",
        "  Returns:\n",
        "    A tuple `(estimator, training_losses, validation_losses)`:\n",
        "      estimator: the trained `DNNRegressor` object.\n",
        "      training_losses: a `list` containing the training loss values taken during training.\n",
        "      validation_losses: a `list` containing the validation loss values taken during training.\n",
        "  \"\"\"\n",
        "\n",
        "  periods = 10\n",
        "  steps_per_period = steps / periods\n",
        "  \n",
        "  # Create a DNNRegressor object.\n",
        "  my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)\n",
        "  dnn_regressor = tf.estimator.DNNRegressor(\n",
        "      feature_columns=construct_feature_columns(training_examples),\n",
        "      hidden_units=hidden_units,\n",
        "      optimizer=my_optimizer\n",
        "  )\n",
        "  \n",
        "  # Create input functions.\n",
        "  training_input_fn = lambda: my_input_fn(training_examples, \n",
        "                                          training_targets[\"median_house_value\"], \n",
        "                                          batch_size=batch_size)\n",
        "  predict_training_input_fn = lambda: my_input_fn(training_examples, \n",
        "                                                  training_targets[\"median_house_value\"], \n",
        "                                                  num_epochs=1, \n",
        "                                                  shuffle=False)\n",
        "  predict_validation_input_fn = lambda: my_input_fn(validation_examples, \n",
        "                                                    validation_targets[\"median_house_value\"], \n",
        "                                                    num_epochs=1, \n",
        "                                                    shuffle=False)\n",
        "\n",
        "  # Train the model, but do so inside a loop so that we can periodically assess\n",
        "  # loss metrics.\n",
        "  print(\"Training model...\")\n",
        "  print(\"RMSE (on training data):\")\n",
        "  training_rmse = []\n",
        "  validation_rmse = []\n",
        "  for period in range (0, periods):\n",
        "    # Train the model, starting from the prior state.\n",
        "    dnn_regressor.train(\n",
        "        input_fn=training_input_fn,\n",
        "        steps=steps_per_period\n",
        "    )\n",
        "    # Take a break and compute predictions.\n",
        "    training_predictions = dnn_regressor.predict(input_fn=predict_training_input_fn)\n",
        "    training_predictions = np.array([item['predictions'][0] for item in training_predictions])\n",
        "    \n",
        "    validation_predictions = dnn_regressor.predict(input_fn=predict_validation_input_fn)\n",
        "    validation_predictions = np.array([item['predictions'][0] for item in validation_predictions])\n",
        "    \n",
        "    # Compute training and validation loss.\n",
        "    training_root_mean_squared_error = math.sqrt(\n",
        "        metrics.mean_squared_error(training_predictions, training_targets))\n",
        "    validation_root_mean_squared_error = math.sqrt(\n",
        "        metrics.mean_squared_error(validation_predictions, validation_targets))\n",
        "    # Occasionally print the current loss.\n",
        "    print(\"  period %02d : %0.2f\" % (period, training_root_mean_squared_error))\n",
        "    # Add the loss metrics from this period to our list.\n",
        "    training_rmse.append(training_root_mean_squared_error)\n",
        "    validation_rmse.append(validation_root_mean_squared_error)\n",
        "  print(\"Model training finished.\")\n",
        "\n",
        "  # Output a graph of loss metrics over periods.\n",
        "  plt.ylabel(\"RMSE\")\n",
        "  plt.xlabel(\"Periods\")\n",
        "  plt.title(\"Root Mean Squared Error vs. Periods\")\n",
        "  plt.tight_layout()\n",
        "  plt.plot(training_rmse, label=\"training\")\n",
        "  plt.plot(validation_rmse, label=\"validation\")\n",
        "  plt.legend()\n",
        "\n",
        "  print(\"Final RMSE (on training data):   %0.2f\" % training_root_mean_squared_error)\n",
        "  print(\"Final RMSE (on validation data): %0.2f\" % validation_root_mean_squared_error)\n",
        "\n",
        "  return dnn_regressor, training_rmse, validation_rmse"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "KueReMZ9Kxk7",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "_ = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0007),\n",
        "    steps=5000,\n",
        "    batch_size=70,\n",
        "    hidden_units=[10, 10],\n",
        "    training_examples=training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "flxmFt0KKxk9",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ## \u7ebf\u6027\u7f29\u653e\n",
        "\u5c06\u8f93\u5165\u6807\u51c6\u5316\u4ee5\u4f7f\u5176\u4f4d\u4e8e (-1, 1) \u8303\u56f4\u5185\u53ef\u80fd\u662f\u4e00\u79cd\u826f\u597d\u7684\u6807\u51c6\u505a\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0cSGD \u5728\u4e00\u4e2a\u7ef4\u5ea6\u4e2d\u91c7\u7528\u5f88\u5927\u6b65\u957f\uff08\u6216\u8005\u5728\u53e6\u4e00\u7ef4\u5ea6\u4e2d\u91c7\u7528\u5f88\u5c0f\u6b65\u957f\uff09\u65f6\u4e0d\u4f1a\u53d7\u963b\u3002\u6570\u503c\u4f18\u5316\u7684\u7231\u597d\u8005\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u8fd9\u79cd\u505a\u6cd5\u4e0e\u4f7f\u7528\u9884\u8c03\u8282\u5668 (Preconditioner) \u7684\u60f3\u6cd5\u662f\u6709\u8054\u7cfb\u7684\u3002"
      ]
    },
    {
      "metadata": {
        "id": "Dws5rIQjKxk-",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def linear_scale(series):\n",
        "  min_val = series.min()\n",
        "  max_val = series.max()\n",
        "  scale = (max_val - min_val) / 2.0\n",
        "  return series.apply(lambda x:((x - min_val) / scale) - 1.0)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "MVmuHI76N2Sz",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \u4efb\u52a1 1\uff1a\u4f7f\u7528\u7ebf\u6027\u7f29\u653e\u5c06\u7279\u5f81\u6807\u51c6\u5316\n",
        "\n",
        "**\u5c06\u8f93\u5165\u6807\u51c6\u5316\u5230 (-1, 1) \u8fd9\u4e00\u8303\u56f4\u5185\u3002**\n",
        "\n",
        "**\u82b1\u8d39 5 \u5206\u949f\u5de6\u53f3\u7684\u65f6\u95f4\u6765\u8bad\u7ec3\u548c\u8bc4\u4f30\u65b0\u6807\u51c6\u5316\u7684\u6570\u636e\u3002\u60a8\u80fd\u8fbe\u5230\u4ec0\u4e48\u7a0b\u5ea6\u7684\u6548\u679c\uff1f**\n",
        "\n",
        "\u4e00\u822c\u6765\u8bf4\uff0c\u5f53\u8f93\u5165\u7279\u5f81\u5927\u81f4\u4f4d\u4e8e\u76f8\u540c\u8303\u56f4\u65f6\uff0c\u795e\u7ecf\u7f51\u7edc\u7684\u8bad\u7ec3\u6548\u679c\u6700\u597d\u3002\n",
        "\n",
        "\u5bf9\u60a8\u7684\u6807\u51c6\u5316\u6570\u636e\u8fdb\u884c\u5065\u5168\u6027\u68c0\u67e5\u3002\uff08\u5982\u679c\u60a8\u5fd8\u4e86\u5c06\u67d0\u4e2a\u7279\u5f81\u6807\u51c6\u5316\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u60c5\u51b5\uff1f\uff09"
      ]
    },
    {
      "metadata": {
        "id": "yD948ZgAM6Cx",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def normalize_linear_scale(examples_dataframe):\n",
        "  \"\"\"Returns a version of the input `DataFrame` that has all its features normalized linearly.\"\"\"\n",
        "  #\n",
        "  # Your code here: normalize the inputs.\n",
        "  #\n",
        "  pass\n",
        "\n",
        "normalized_dataframe = normalize_linear_scale(preprocess_features(california_housing_dataframe))\n",
        "normalized_training_examples = normalized_dataframe.head(12000)\n",
        "normalized_validation_examples = normalized_dataframe.tail(5000)\n",
        "\n",
        "_ = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0007),\n",
        "    steps=5000,\n",
        "    batch_size=70,\n",
        "    hidden_units=[10, 10],\n",
        "    training_examples=normalized_training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=normalized_validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "jFfc3saSxg6t",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \u89e3\u51b3\u65b9\u6848\n",
        "\n",
        "\u70b9\u51fb\u4e0b\u65b9\u5373\u53ef\u67e5\u770b\u4e00\u79cd\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002"
      ]
    },
    {
      "metadata": {
        "id": "Ax_IIQVRx4gr",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u7531\u4e8e\u6807\u51c6\u5316\u4f1a\u4f7f\u7528\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6574\u4e2a\u6570\u636e\u96c6\u4e2d\u4e00\u6b21\u6027\u5b8c\u6210\u8be5\u64cd\u4f5c\u3002\n",
        "\n",
        "\u6211\u4eec\u4e4b\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u662f\u56e0\u4e3a\u6211\u4eec\u6240\u6709\u7684\u6570\u636e\u90fd\u5728\u4e00\u4e2a DataFrame \u4e2d\u3002\u5982\u679c\u6211\u4eec\u6709\u591a\u4e2a\u6570\u636e\u96c6\uff0c\u5219\u6700\u597d\u4ece\u8bad\u7ec3\u96c6\u4e2d\u5bfc\u51fa\u6807\u51c6\u5316\u53c2\u6570\uff0c\u7136\u540e\u4ee5\u76f8\u540c\u65b9\u5f0f\u5c06\u5176\u5e94\u7528\u4e8e\u6d4b\u8bd5\u96c6\u3002"
      ]
    },
    {
      "metadata": {
        "id": "D-bJBXrJx-U_",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def normalize_linear_scale(examples_dataframe):\n",
        "  \"\"\"Returns a version of the input `DataFrame` that has all its features normalized linearly.\"\"\"\n",
        "  processed_features = pd.DataFrame()\n",
        "  processed_features[\"latitude\"] = linear_scale(examples_dataframe[\"latitude\"])\n",
        "  processed_features[\"longitude\"] = linear_scale(examples_dataframe[\"longitude\"])\n",
        "  processed_features[\"housing_median_age\"] = linear_scale(examples_dataframe[\"housing_median_age\"])\n",
        "  processed_features[\"total_rooms\"] = linear_scale(examples_dataframe[\"total_rooms\"])\n",
        "  processed_features[\"total_bedrooms\"] = linear_scale(examples_dataframe[\"total_bedrooms\"])\n",
        "  processed_features[\"population\"] = linear_scale(examples_dataframe[\"population\"])\n",
        "  processed_features[\"households\"] = linear_scale(examples_dataframe[\"households\"])\n",
        "  processed_features[\"median_income\"] = linear_scale(examples_dataframe[\"median_income\"])\n",
        "  processed_features[\"rooms_per_person\"] = linear_scale(examples_dataframe[\"rooms_per_person\"])\n",
        "  return processed_features\n",
        "\n",
        "normalized_dataframe = normalize_linear_scale(preprocess_features(california_housing_dataframe))\n",
        "normalized_training_examples = normalized_dataframe.head(12000)\n",
        "normalized_validation_examples = normalized_dataframe.tail(5000)\n",
        "\n",
        "_ = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.005),\n",
        "    steps=2000,\n",
        "    batch_size=50,\n",
        "    hidden_units=[10, 10],\n",
        "    training_examples=normalized_training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=normalized_validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "MrwtdStNJ6ZQ",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \u4efb\u52a1 2\uff1a\u5c1d\u8bd5\u5176\u4ed6\u4f18\u5316\u5668\n",
        "\n",
        "** \u4f7f\u7528 AdaGrad \u548c Adam \u4f18\u5316\u5668\u5e76\u5bf9\u6bd4\u5176\u6548\u679c\u3002**\n",
        "\n",
        "AdaGrad \u4f18\u5316\u5668\u662f\u4e00\u79cd\u5907\u9009\u65b9\u6848\u3002AdaGrad \u7684\u6838\u5fc3\u662f\u7075\u6d3b\u5730\u4fee\u6539\u6a21\u578b\u4e2d\u6bcf\u4e2a\u7cfb\u6570\u7684\u5b66\u4e60\u7387\uff0c\u4ece\u800c\u5355\u8c03\u964d\u4f4e\u6709\u6548\u7684\u5b66\u4e60\u7387\u3002\u8be5\u4f18\u5316\u5668\u5bf9\u4e8e\u51f8\u4f18\u5316\u95ee\u9898\u975e\u5e38\u6709\u6548\uff0c\u4f46\u4e0d\u4e00\u5b9a\u9002\u5408\u975e\u51f8\u4f18\u5316\u95ee\u9898\u7684\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a `AdagradOptimizer`\uff08\u800c\u4e0d\u662f `GradientDescentOptimizer`\uff09\u6765\u4f7f\u7528 AdaGrad\u3002\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e AdaGrad\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u8f83\u5927\u7684\u5b66\u4e60\u7387\u3002\n",
        "\n",
        "\u5bf9\u4e8e\u975e\u51f8\u4f18\u5316\u95ee\u9898\uff0cAdam \u6709\u65f6\u6bd4 AdaGrad \u66f4\u6709\u6548\u3002\u8981\u4f7f\u7528 Adam\uff0c\u8bf7\u8c03\u7528 `tf.train.AdamOptimizer` \u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u5c06\u51e0\u4e2a\u53ef\u9009\u8d85\u53c2\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u4f46\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\u4ec5\u6307\u5b9a\u5176\u4e2d\u4e00\u4e2a (`learning_rate`)\u3002\u5728\u5e94\u7528\u8bbe\u7f6e\u4e2d\uff0c\u60a8\u5e94\u8be5\u8c28\u614e\u6307\u5b9a\u548c\u8c03\u6574\u53ef\u9009\u8d85\u53c2\u6570\u3002"
      ]
    },
    {
      "metadata": {
        "id": "61GSlDvF7-7q",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "#\n",
        "# YOUR CODE HERE: Retrain the network using Adagrad and then Adam.\n",
        "#"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "FSPZIiYgyh93",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \u89e3\u51b3\u65b9\u6848\n",
        "\n",
        "\u70b9\u51fb\u4e0b\u65b9\u5373\u53ef\u67e5\u770b\u89e3\u51b3\u65b9\u6848"
      ]
    },
    {
      "metadata": {
        "id": "X1QcIeiKyni4",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u9996\u5148\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5 AdaGrad\u3002"
      ]
    },
    {
      "metadata": {
        "id": "Ntn4jJxnypGZ",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "_, adagrad_training_losses, adagrad_validation_losses = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.AdagradOptimizer(learning_rate=0.5),\n",
        "    steps=500,\n",
        "    batch_size=100,\n",
        "    hidden_units=[10, 10],\n",
        "    training_examples=normalized_training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=normalized_validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "5JUsCdRRyso3",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u73b0\u5728\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5 Adam\u3002"
      ]
    },
    {
      "metadata": {
        "id": "lZB8k0upyuY8",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "_, adam_training_losses, adam_validation_losses = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.AdamOptimizer(learning_rate=0.009),\n",
        "    steps=500,\n",
        "    batch_size=100,\n",
        "    hidden_units=[10, 10],\n",
        "    training_examples=normalized_training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=normalized_validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "twYgC8FGyxm6",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u6211\u4eec\u5e76\u6392\u8f93\u51fa\u635f\u5931\u6307\u6807\u7684\u56fe\u8868\u3002"
      ]
    },
    {
      "metadata": {
        "id": "8RHIUEfqyzW0",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "plt.ylabel(\"RMSE\")\n",
        "plt.xlabel(\"Periods\")\n",
        "plt.title(\"Root Mean Squared Error vs. Periods\")\n",
        "plt.plot(adagrad_training_losses, label='Adagrad training')\n",
        "plt.plot(adagrad_validation_losses, label='Adagrad validation')\n",
        "plt.plot(adam_training_losses, label='Adam training')\n",
        "plt.plot(adam_validation_losses, label='Adam validation')\n",
        "_ = plt.legend()"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "UySPl7CAQ28C",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \u4efb\u52a1 3\uff1a\u5c1d\u8bd5\u5176\u4ed6\u6807\u51c6\u5316\u65b9\u6cd5\n",
        "\n",
        "**\u5c1d\u8bd5\u5bf9\u5404\u79cd\u7279\u5f81\u4f7f\u7528\u5176\u4ed6\u6807\u51c6\u5316\u65b9\u6cd5\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6027\u80fd\u3002**\n",
        "\n",
        "\u5982\u679c\u4ed4\u7ec6\u67e5\u770b\u8f6c\u6362\u540e\u6570\u636e\u7684\u6c47\u603b\u7edf\u8ba1\u4fe1\u606f\uff0c\u60a8\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u5bf9\u67d0\u4e9b\u7279\u5f81\u8fdb\u884c\u7ebf\u6027\u7f29\u653e\u4f1a\u4f7f\u5176\u805a\u96c6\u5230\u63a5\u8fd1 `-1` \u7684\u4f4d\u7f6e\u3002\n",
        "\n",
        "\u4f8b\u5982\uff0c\u5f88\u591a\u7279\u5f81\u7684\u4e2d\u4f4d\u6570\u7ea6\u4e3a `-0.8`\uff0c\u800c\u4e0d\u662f `0.0`\u3002"
      ]
    },
    {
      "metadata": {
        "id": "QWmm_6CGKxlH",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "_ = training_examples.hist(bins=20, figsize=(18, 12), xlabelsize=2)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Xx9jgEMHKxlJ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u901a\u8fc7\u9009\u62e9\u5176\u4ed6\u65b9\u5f0f\u6765\u8f6c\u6362\u8fd9\u4e9b\u7279\u5f81\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002\n",
        "\n",
        "\u4f8b\u5982\uff0c\u5bf9\u6570\u7f29\u653e\u53ef\u80fd\u5bf9\u67d0\u4e9b\u7279\u5f81\u6709\u5e2e\u52a9\u3002\u6216\u8005\uff0c\u622a\u53d6\u6781\u7aef\u503c\u53ef\u80fd\u4f1a\u4f7f\u5269\u4f59\u90e8\u5206\u7684\u4fe1\u606f\u66f4\u52a0\u4e30\u5bcc\u3002"
      ]
    },
    {
      "metadata": {
        "id": "baKZa6MEKxlK",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def log_normalize(series):\n",
        "  return series.apply(lambda x:math.log(x+1.0))\n",
        "\n",
        "def clip(series, clip_to_min, clip_to_max):\n",
        "  return series.apply(lambda x:(\n",
        "    min(max(x, clip_to_min), clip_to_max)))\n",
        "\n",
        "def z_score_normalize(series):\n",
        "  mean = series.mean()\n",
        "  std_dv = series.std()\n",
        "  return series.apply(lambda x:(x - mean) / std_dv)\n",
        "\n",
        "def binary_threshold(series, threshold):\n",
        "  return series.apply(lambda x:(1 if x > threshold else 0))"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "-wCCq_ClKxlO",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u4e0a\u8ff0\u90e8\u5206\u5305\u542b\u4e00\u4e9b\u989d\u5916\u7684\u6807\u51c6\u5316\u51fd\u6570\u3002\u8bf7\u5c1d\u8bd5\u5176\u4e2d\u7684\u67d0\u4e9b\u51fd\u6570\uff0c\u6216\u6dfb\u52a0\u60a8\u81ea\u5df1\u7684\u51fd\u6570\u3002\n",
        "\n",
        "\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u60a8\u5c06\u76ee\u6807\u6807\u51c6\u5316\uff0c\u5219\u9700\u8981\u5c06\u7f51\u7edc\u7684\u9884\u6d4b\u7ed3\u679c\u975e\u6807\u51c6\u5316\uff0c\u4ee5\u4fbf\u6bd4\u8f83\u635f\u5931\u51fd\u6570\u7684\u503c\u3002"
      ]
    },
    {
      "metadata": {
        "id": "8ToG-mLfMO9P",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def normalize(examples_dataframe):\n",
        "  \"\"\"Returns a version of the input `DataFrame` that has all its features normalized.\"\"\"\n",
        "  #\n",
        "  # YOUR CODE HERE: Normalize the inputs.\n",
        "  #\n",
        "  pass\n",
        "\n",
        "normalized_dataframe = normalize(preprocess_features(california_housing_dataframe))\n",
        "normalized_training_examples = normalized_dataframe.head(12000)\n",
        "normalized_validation_examples = normalized_dataframe.tail(5000)\n",
        "\n",
        "_ = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0007),\n",
        "    steps=5000,\n",
        "    batch_size=70,\n",
        "    hidden_units=[10, 10],\n",
        "    training_examples=normalized_training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=normalized_validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "GhFtWjQRzD2l",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \u89e3\u51b3\u65b9\u6848\n",
        "\n",
        "\u70b9\u51fb\u4e0b\u65b9\u5373\u53ef\u67e5\u770b\u4e00\u79cd\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002"
      ]
    },
    {
      "metadata": {
        "id": "OMoIsUMmzK9b",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u4ee5\u4e0a\u8fd9\u4e9b\u53ea\u662f\u6211\u4eec\u80fd\u60f3\u5230\u7684\u5904\u7406\u6570\u636e\u7684\u51e0\u79cd\u65b9\u6cd5\u3002\u5176\u4ed6\u8f6c\u6362\u65b9\u5f0f\u53ef\u80fd\u4f1a\u66f4\u597d\uff01\n",
        "\n",
        "`households`\u3001`median_income` \u548c `total_bedrooms` \u5728\u5bf9\u6570\u7a7a\u95f4\u5185\u5747\u5448\u73b0\u4e3a\u6b63\u6001\u5206\u5e03\u3002\n",
        "\n",
        "\u5982\u679c `latitude`\u3001`longitude` \u548c `housing_median_age` \u50cf\u4e4b\u524d\u4e00\u6837\u8fdb\u884c\u7ebf\u6027\u7f29\u653e\uff0c\u6548\u679c\u53ef\u80fd\u4f1a\u66f4\u597d\u3002\n",
        "\n",
        "`population`\u3001`totalRooms` \u548c `rooms_per_person` \u5177\u6709\u51e0\u4e2a\u6781\u7aef\u79bb\u7fa4\u503c\u3002\u8fd9\u4e9b\u503c\u4f3c\u4e4e\u8fc7\u4e8e\u6781\u7aef\uff0c\u4ee5\u81f3\u4e8e\u6211\u4eec\u65e0\u6cd5\u5229\u7528\u5bf9\u6570\u6807\u51c6\u5316\u5904\u7406\u8fd9\u4e9b\u79bb\u7fa4\u503c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u76f4\u63a5\u622a\u53d6\u6389\u8fd9\u4e9b\u503c\u3002"
      ]
    },
    {
      "metadata": {
        "id": "XDEYkPquzYCH",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def normalize(examples_dataframe):\n",
        "  \"\"\"Returns a version of the input `DataFrame` that has all its features normalized.\"\"\"\n",
        "  processed_features = pd.DataFrame()\n",
        "\n",
        "  processed_features[\"households\"] = log_normalize(examples_dataframe[\"households\"])\n",
        "  processed_features[\"median_income\"] = log_normalize(examples_dataframe[\"median_income\"])\n",
        "  processed_features[\"total_bedrooms\"] = log_normalize(examples_dataframe[\"total_bedrooms\"])\n",
        "  \n",
        "  processed_features[\"latitude\"] = linear_scale(examples_dataframe[\"latitude\"])\n",
        "  processed_features[\"longitude\"] = linear_scale(examples_dataframe[\"longitude\"])\n",
        "  processed_features[\"housing_median_age\"] = linear_scale(examples_dataframe[\"housing_median_age\"])\n",
        "\n",
        "  processed_features[\"population\"] = linear_scale(clip(examples_dataframe[\"population\"], 0, 5000))\n",
        "  processed_features[\"rooms_per_person\"] = linear_scale(clip(examples_dataframe[\"rooms_per_person\"], 0, 5))\n",
        "  processed_features[\"total_rooms\"] = linear_scale(clip(examples_dataframe[\"total_rooms\"], 0, 10000))\n",
        "\n",
        "  return processed_features\n",
        "\n",
        "normalized_dataframe = normalize(preprocess_features(california_housing_dataframe))\n",
        "normalized_training_examples = normalized_dataframe.head(12000)\n",
        "normalized_validation_examples = normalized_dataframe.tail(5000)\n",
        "\n",
        "_ = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.AdagradOptimizer(learning_rate=0.15),\n",
        "    steps=1000,\n",
        "    batch_size=50,\n",
        "    hidden_units=[10, 10],\n",
        "    training_examples=normalized_training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=normalized_validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "b7atJTbzU9Ca",
        "colab_type": "text",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "cell_type": "markdown",
      "source": [
        " ## \u53ef\u9009\u6311\u6218\uff1a\u4ec5\u4f7f\u7528\u7eac\u5ea6\u548c\u7ecf\u5ea6\u7279\u5f81\n",
        "\n",
        "**\u8bad\u7ec3\u4ec5\u4f7f\u7528\u7eac\u5ea6\u548c\u7ecf\u5ea6\u4f5c\u4e3a\u7279\u5f81\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002**\n",
        "\n",
        "\u623f\u5730\u4ea7\u5546\u559c\u6b22\u8bf4\uff0c\u5730\u6bb5\u662f\u623f\u4ef7\u7684\u552f\u4e00\u91cd\u8981\u7279\u5f81\u3002\n",
        "\u6211\u4eec\u6765\u770b\u770b\u80fd\u5426\u901a\u8fc7\u8bad\u7ec3\u4ec5\u4f7f\u7528\u7eac\u5ea6\u548c\u7ecf\u5ea6\u4f5c\u4e3a\u7279\u5f81\u7684\u6a21\u578b\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002\n",
        "\n",
        "\u53ea\u6709\u6211\u4eec\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u53ef\u4ee5\u4ece\u7eac\u5ea6\u548c\u7ecf\u5ea6\u4e2d\u5b66\u4f1a\u590d\u6742\u7684\u975e\u7ebf\u6027\u89c4\u5f8b\uff0c\u624d\u80fd\u8fbe\u5230\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u3002\n",
        "\n",
        "**\u6ce8\u610f**\uff1a\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u7f51\u7edc\u7ed3\u6784\uff0c\u5176\u5c42\u6570\u6bd4\u6211\u4eec\u4e4b\u524d\u5728\u7ec3\u4e60\u4e2d\u4f7f\u7528\u7684\u8981\u591a\u3002"
      ]
    },
    {
      "metadata": {
        "id": "T5McjahpamOc",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "#\n",
        "# YOUR CODE HERE: Train the network using only latitude and longitude\n",
        "#"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "P8BLQ7T71JWd",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " ### \u89e3\u51b3\u65b9\u6848\n",
        "\n",
        "\u70b9\u51fb\u4e0b\u65b9\u5373\u53ef\u67e5\u770b\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002"
      ]
    },
    {
      "metadata": {
        "id": "1hwaFCE71OPZ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u6700\u597d\u4f7f\u7eac\u5ea6\u548c\u7ecf\u5ea6\u4fdd\u6301\u6807\u51c6\u5316\u72b6\u6001\uff1a"
      ]
    },
    {
      "metadata": {
        "id": "djKtt4mz1ZEc",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "source": [
        "def location_location_location(examples_dataframe):\n",
        "  \"\"\"Returns a version of the input `DataFrame` that keeps only the latitude and longitude.\"\"\"\n",
        "  processed_features = pd.DataFrame()\n",
        "  processed_features[\"latitude\"] = linear_scale(examples_dataframe[\"latitude\"])\n",
        "  processed_features[\"longitude\"] = linear_scale(examples_dataframe[\"longitude\"])\n",
        "  return processed_features\n",
        "\n",
        "lll_dataframe = location_location_location(preprocess_features(california_housing_dataframe))\n",
        "lll_training_examples = lll_dataframe.head(12000)\n",
        "lll_validation_examples = lll_dataframe.tail(5000)\n",
        "\n",
        "_ = train_nn_regression_model(\n",
        "    my_optimizer=tf.train.AdagradOptimizer(learning_rate=0.05),\n",
        "    steps=500,\n",
        "    batch_size=50,\n",
        "    hidden_units=[10, 10, 5, 5, 5],\n",
        "    training_examples=lll_training_examples,\n",
        "    training_targets=training_targets,\n",
        "    validation_examples=lll_validation_examples,\n",
        "    validation_targets=validation_targets)"
      ],
      "cell_type": "code",
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Dw2Mr9JZ1cRi",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        " \u5bf9\u4e8e\u53ea\u6709\u4e24\u4e2a\u7279\u5f81\u7684\u6a21\u578b\uff0c\u7ed3\u679c\u5e76\u4e0d\u7b97\u592a\u7cdf\u3002\u5f53\u7136\uff0c\u5730\u4ea7\u4ef7\u503c\u5728\u77ed\u8ddd\u79bb\u5185\u4ecd\u7136\u53ef\u80fd\u6709\u8f83\u5927\u5dee\u5f02\u3002"
      ]
    }
  ]
}
